function [mydemo, cleanup] = demo_get_data
%
% Demo of RBF design matrix utility.
%


% Initialise.
n = 0;

n = n + 1;
mydemo(n) = struct( ...
  'comments', {{
              'Howdy! This is the demo for the get_data utility.', ...
              '-------------------------------------------------', ...
              '', ...
              'get_data is used to create training or test sets for', ...
              'a small set of simple simulated regression problems.'}}, ...
  'commands', '', ...
  'question', '', ...
  'optional', '');

n = n + 1;
mydemo(n) = struct( ...
  'comments', 'First, let''s find out what problems get_data can handle.', ...
  'commands', 'get_data(''names'')', ...
  'question', {{...
              'This shows that, amongst others, get_data recognizes', ...
              'two problems called ''sine1'' and ''hermite'', both of', ...
              'which map 1D inputs to 1D outputs.', ...
              '', ...
              'Please pick one of these 1D-to-1D problems.'}}, ...
  'optional', {{'sine1', 'hermite'}});

n = n + 1;
mydemo(n) = struct( ...
  'comments', {{ ...
              'Okay, you chose ''answer''. So now let''s run get_data on', ...
              'that problem and let it set the configuration defaults itself.'}}, ...
  'commands', '[x, y, conf] = get_data(''answer'');', ...
  'question', '', ...
  'optional', '');

n = n + 1;
mydemo(n) = struct( ...
  'comments', 'Let''s have a look at the x and y values get_data has returned.', ...
  'commands', {{ ...
              'fig = get_fig(''get_data demo'');', ...
              'hold off', ...
              'plot(x, y, ''r*'')', ...
              '%set(gca, ''XLim'', [conf.x1 conf.x2])', ...
              '%set(gca, ''YLim'', [floor(min(y)) ceil(max(y))])', ...
              '%set(gca, ''XTick'', conf.x1:conf.x2)', ...
              '%set(gca, ''YTick'', floor(min(y)):ceil(max(y)))', ...
              '%xlabel(''x'', ''FontSize'', 16)', ...
              '%ylabel(''y'', ''FontSize'', 16, ''Rotation'', 0)'}}, ...
  'question', '', ...
  'optional', '');

n = n + 1;
mydemo(n) = struct( ...
  'comments', {{ ...
              'This noisy data is obviously meant to be a training set.', ...
              'You can check on that by examining some of the defaults', ...
              'that get_data has set and passed back in conf. For example,', ...
              'the noise (conf.std) and whether the input values are ordered', ...
              '(conf.ord: 1 means ordered and evenly spaced, 0 means not', ...
              'ordered and not evenly spaced).'}}, ...
  'commands', {{ ...
              'disp(conf.std)', ...
              'disp(conf.ord)'}}, ...
  'question', '', ...
  'optional', '');

n = n + 1;
mydemo(n) = struct( ...
  'comments', {{ ...
              'Next, let''s create a test set to go with the training set', ...
              'we''ve already got. Our test set will be uncorrupted by noise', ...
              'and we want the x values to be ordered (for plotting purposes).', ...
              'To achieve this we can just edit the conf structure and hand it', ...
              'back to get data.'}}, ...
  'commands', {{ ...
              'conf.std = 0;', ...
              'conf.ord = 1;', ...
              '[xt, yt] = get_data(conf);', ...
              'hold on', ...
              'plot(xt, yt, ''b-'', ''LineWidth'', 2)'}}, ...
  'question', '', ...
  'optional', '');

n = n + 1;
mydemo(n) = struct( ...
  'comments', {{ ...
              'If you want to plot a curve then ordering the inputs (x values)', ...
              'is important, otherwise this happens. Not a pretty sight!'}}, ...
  'commands', {{ ...
              'conf.ord = 0;', ...
              '[xt, yt] = get_data(conf);', ...
              'hold off', ...
              'plot(xt, yt, ''b-'', ''LineWidth'', 2)', ...
              '%set(gca, ''XLim'', [conf.x1 conf.x2])', ...
              '%set(gca, ''YLim'', [floor(min(y)) ceil(max(y))])', ...
              '%set(gca, ''XTick'', conf.x1:conf.x2)', ...
              '%set(gca, ''YTick'', floor(min(y)):ceil(max(y)))', ...
              '%xlabel(''x'', ''FontSize'', 16)', ...
              '%ylabel(''y'', ''FontSize'', 16, ''Rotation'', 0)'}}, ...
  'question', '', ...
  'optional', '');

n = n + 1;
mydemo(n) = struct( ...
  'comments', 'Of course, if all you want is points then that''s no problem.', ...
  'commands', {{ ...
              'plot(xt, yt, ''b*'')', ...
              '%set(gca, ''XLim'', [conf.x1 conf.x2])', ...
              '%set(gca, ''YLim'', [floor(min(y)) ceil(max(y))])', ...
              '%set(gca, ''XTick'', conf.x1:conf.x2)', ...
              '%set(gca, ''YTick'', floor(min(y)):ceil(max(y)))', ...
              '%xlabel(''x'', ''FontSize'', 16)', ...
              '%ylabel(''y'', ''FontSize'', 16, ''Rotation'', 0)'}}, ...
  'question', '', ...
  'optional', '');

n = n + 1;
mydemo(n) = struct( ...
  'comments', {{ ...
              'What we just saw was an example where the input space (x) was 1D.', ...
              'Plotting 2D data sets is a little more tricky.'}}, ...
  'commands', '', ...
  'question', 'Would you like to see a 2D example?', ...
  'optional', {{'yes', 'quit'}});

n = n + 1;
mydemo(n) = struct( ...
  'comments', {{ ...
              'Okay, let''s try the ''sine2'' problem and, at first, allow', ...
              'get_data to choose all the defaults. We''ll plot the positions', ...
              'of the input points in the 2D plane.'}}, ...
  'commands', {{ ...
              '[X, y, conf] = get_data(''sine2'');', ...
              'plot(X(1,:), X(2,:), ''m*'')', ...
              '%xlabel(''x_1'', ''FontSize'', 16)', ...
              '%ylabel(''x_2'', ''FontSize'', 16, ''Rotation'', 0)'}}, ...
  'question', '', ...
  'optional', '');

n = n + 1;
mydemo(n) = struct( ...
  'comments', {{ ...
              'How many samples (conf.p) has get_data given us, are the inputs', ...
              'ordered (conf.ord) and how much noise (conf.std) is corrupting', ...
              'the outputvalues?'}}, ...
  'commands', {{ ...
              'disp(conf.p)', ...
              'disp(conf.ord)', ...
              'disp(conf.std)'}}, ...
  'question', '', ...
  'optional', '');

n = n + 1;
mydemo(n) = struct( ...
  'comments', {{ ...
              'Ordering is turned off (as you could see from the figure) which is a', ...
              'problem for plotting the surface. Matlab uses ordered and regularly', ...
              'spaced grids for plotting 2D functions so we''ll need to turn ordering', ...
              'on (which will also have the effect of regularly spacing out the', ...
              'x-component values). In addition, we want an uncorrupted (smooth)', ...
              'surface so we''ll set the noise to zero.'}}, ...
  'commands', {{ ...
              'conf.ord = 1;', ...
              'conf.std = 0;', ...
              '[X, y, conf] = get_data(conf);', ...
              'plot(X(1,:), X(2,:), ''m*'')', ...
              '%xlabel(''x_1'', ''FontSize'', 16)', ...
              '%ylabel(''x_2'', ''FontSize'', 16, ''Rotation'', 0)'}}, ...
  'question', '', ...
  'optional', '');

n = n + 1;
mydemo(n) = struct( ...
  'comments', {{ ...
              'Notice the number of samples (conf.p) has changed, even though we', ...
              'didn''t explicitly alter it. Why? To get an even number of points', ...
              'along each of the two input dimensions get_data has taken the', ...
              'square root of the original number, rounded it and squared it.', ...
              'The new number of samples is the nearest perfect square to the', ...
              'original number.'}}, ...
  'commands', 'disp(conf.p)', ...
  'question', '', ...
  'optional', '');

n = n + 1;
mydemo(n) = struct( ...
  'comments', {{ ...
              'Before we can plot the surface we also need to turn the output', ...
              'vector (y) into a matrix and infer the x-component values that', ...
              'get_data has assigned. The latter can be accomplished by finding', ...
              'out the minimum (conf.x1) and maximum (conf.x2) x-values.'}}, ...
  'commands', {{ ...
              'q = sqrt(conf.p);', ...
              'Y = zeros(q,q);', ...
              'Y(:) = y;', ...
              'v1 = linspace(conf.x1(1), conf.x2(1), q);', ...
              'v2 = linspace(conf.x1(2), conf.x2(2), q);'}}, ...
  'question', '', ...
  'optional', '');

n = n + 1;
mydemo(n) = struct( ...
  'comments', {{ ...
              'Now at last we''re ready to do the plotting.', ...
              'First a fairly standard effort.'}}, ...
  'commands', {{ ...
              'mesh(v1, v2, Y)', ...
              '%xlabel(''x_1'', ''FontSize'', 16)', ...
              '%ylabel(''x_2'', ''FontSize'', 16)', ...
              '%zlabel(''y'', ''FontSize'', 16, ''Rotation'', 0)'}}, ...
  'question', '', ...
  'optional', '');

n = n + 1;
mydemo(n) = struct( ...
  'comments', 'Secondly, a more ambitious one.', ...
  'commands', {{ ...
              'surfl(v1, v2, Y)', ...
              'light(''Position'', [0 0 1], ''Style'', ''infinite'');', ...
              'colormap(hot)', ...
              'shading interp', ...
              '%xlabel(''x_1'', ''FontSize'', 16)', ...
              '%ylabel(''x_2'', ''FontSize'', 16)', ...
              '%zlabel(''y'', ''FontSize'', 16, ''Rotation'', 0)'}}, ...
  'question', '', ...
  'optional', '');

n = n + 1;
mydemo(n) = struct( ...
  'comments', {{ ...
              'Finally, let''s change one of the function''s parameters.', ...
	      'The contents of the fields ''pinfo'' and ''par'' in conf', ...
              'tell you what they''re for and what their values are.', ...
              'In this example we''ll change the frequency along x_1.'}}, ...
  'commands', {{ ...
              'disp(conf.pinfo)', ...
              'disp(conf.par)', ...
              'conf.par(2) = 1;', ...
              '[X, y] = get_data(conf);', ...
              'Y(:) = y;', ...
              'surfl(v1, v2, Y)', ...
              '%light(''Position'', [0 0 1], ''Style'', ''infinite'');', ...
              '%colormap(hot)', ...
              '%shading interp', ...
              '%xlabel(''x_1'', ''FontSize'', 16)', ...
              '%ylabel(''x_2'', ''FontSize'', 16)', ...
              '%zlabel(''y'', ''FontSize'', 16, ''Rotation'', 0)'}}, ...
  'question', '', ...
  'optional', '');

n = n + 1;
mydemo(n) = struct( ...
  'comments', 'End of get_data demo.', ...
  'commands', '', ...
  'question', '', ...
  'optional', '');

% Define the commands necessary to cleanup after the demo (e.g. close figures).
cleanup = 'if exist(''fig'', ''var'') if ~isempty(find(findobj(''type'',''figure'') == fig)) close(fig); end; end';
